package com.kitty.game.fight.factory.model;

import com.kitty.common.model.Pos;
import com.kitty.game.role.model.Role;
import com.kitty.game.fight.bean.Fight;
import com.kitty.game.fight.bean.FightObject;
import com.kitty.game.fight.bean.MonsterFightObject;
import com.kitty.game.config.PetSet;
import com.kitty.common.utils.SpringUtils;
import com.kitty.common.utils.PositionUtils;
import com.kitty.game.fight.message.RespCombatFriends;
import com.kitty.game.fight.message.RespCombatOpponets;
import com.kitty.game.fight.message.vo.FightMember;
import com.kitty.game.team.model.Member;
import com.kitty.game.team.model.Team;
import com.kitty.game.utils.Const;
import com.kitty.game.fight.service.MonsterService;
import org.nutz.lang.util.NutMap;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class WildMonsterFight extends Fight {

    private Team team;

    private Role role;

    private List<FightMember> fightMemberA = new ArrayList<>();
    private List<FightMember> fightMemberB = new ArrayList<>();

    private List<PetSet> monsters;

    private  List<Member> fightMembers;

    public WildMonsterFight(Team team, Role role) {
        super(Const.fightType_monster);
        Pos position = role.getPos();
        int mapId = position.getMapId();
        setMapId(mapId);
        this.role = role;
        this.team = team;
        MonsterService monsterService = SpringUtils.getBean(MonsterService.class);
        monsters = monsterService.getMapMonster(mapId);
    }

    /**
     * 加载战场所有战斗单元
     */
    protected void loadActors() {
        fightMembers = fightService.loadTeamBattleInfo(team, fightMemberA, this);

        ArrayList<PetSet> petSets = createRandPetSet(team, monsters, fightMembers);

        loadMonsterBattleInfo(petSets, fightMemberB, this);
    }

    private ArrayList<PetSet> createRandPetSet(Team team, List<PetSet> monsters, List<Member> list) {
        Random random = new Random();
        int count = 0;
        int memberNum = team.getShouhus().size() + list.size();
        if (memberNum == 1) {
            count = random.nextInt(2) + 1;
        } else if (memberNum == 2) {
            count = random.nextInt(3) + 2;
        } else if (memberNum == 3) {
            count = random.nextInt(4) + 3;
        } else if (memberNum == 4) {
            count = random.nextInt(5) + 4;
        } else {
            count = random.nextInt(6) + 5;
        }
//        count=10;
        ArrayList<PetSet> result = new ArrayList<>();
        do {
            int i = random.nextInt(monsters.size());
            result.add(monsters.get(i));
        } while (result.size() < count);
        return result;
    }

    /**
     * 加载怪物信息
     */
    private void loadMonsterBattleInfo(List<PetSet> petSets, List<FightMember> monsterListB, Fight battleground) {
        int index = 0;
        List<FightObject> memberListB = battleground.getListB();
        int monsterId = 567898745;
        NutMap expMap = NutMap.NEW();
        expMap.setv("level", petSets.get(0).getLevel());
        for (PetSet petSet : petSets) {
            FightObject fightObject = new MonsterFightObject(petSet, PositionUtils.positions.get(index), monsterId);
            FightMember fightMember = fightObject.asFightMember();
            monsterListB.add(fightMember);
            memberListB.add(fightObject);
            index++;
            monsterId++;

            // 写入野外战斗怪物给的经验
            int exp = expMap.getInt("exp", 0);
            expMap.setv("exp", exp + petSet.getLevel() * petSet.getExp());
            battleground.setExpMap(expMap);
        }
    }

    protected void notifyAndStart() {

        /**友方*/
        RespCombatFriends respCombatFriends = new RespCombatFriends();
        respCombatFriends.setCustomIconList(getCustomIconListA());

        /**敌方*/
        RespCombatOpponets respCombatOpponets = new RespCombatOpponets();
        respCombatOpponets.setCustomIconList(getCustomIconListB());

        respCombatOpponets.setList(fightMemberB);
        respCombatFriends.setList(fightMemberA);


        fightService.sendStartCombat(respCombatFriends, respCombatOpponets, fightMembers);
        fightService.whileSendStart(this);

        for (FightObject fightObject : getListA()) {
            if (fightObject.isPlayer()) {
                fightService.autoFightOne(this, fightObject);
            }
        }
    }
}
